//
//  SetVC.m
//  Mahjong
//
//  Created by 饶首建 on 17/6/21.
//  Copyright © 2017年 com.ikamobile.com. All rights reserved.
//

#import "SetVC.h"
#import "MainRecordVC.h"

#import "SetCollectionViewCell.h"
#import "PatternModel.h"

@interface SetVC () <UIPickerViewDelegate,UIPickerViewDataSource,UICollectionViewDataSource,UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIPickerView *ceilingPickerView;
@property (weak, nonatomic) IBOutlet UIPickerView *basePickerView;

@property (nonatomic,strong) NSArray *baseDataSource;
@property (nonatomic,strong) NSArray *ceilingDataSource;
@property (nonatomic,strong) NSMutableArray *selectedRuleArr;

@property (nonatomic,strong) PatternModel *patternModel;
@property (nonatomic,copy) NSString *selectedBase;
@property (nonatomic,copy) NSString *selectedCeiling;


@end
static NSString *kSetCollectionViewCell = @"kSetCollectionViewCell";
@implementation SetVC

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if (!_patternModel) {
        [PatternModel loadRuleSuccess:^(id model) {
            _patternModel = model;
            _selectedRuleArr = [NSMutableArray new];
            [_collectionView reloadData];
        } fail:^(NSString *errorStr) {
            
        }];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _selectedRuleArr = [NSMutableArray new];
    _baseDataSource = @[@"1",@"2",@"3",@"4",@"5",@"10",@"20"];
    _ceilingDataSource = @[@"1",@"2",@"3",@"4",@"5",@"不限"];
    [_basePickerView selectRow:1 inComponent:0 animated:YES];
    _selectedBase = _baseDataSource[1];
    [_ceilingPickerView selectRow:3 inComponent:0 animated:YES];
    _selectedCeiling = _ceilingDataSource[3];
    
    [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([SetCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:kSetCollectionViewCell];
}

- (IBAction)createMatchAction:(id)sender {
    NSString *url = [BASE_URL stringByAppendingPathComponent:API_POST_JOIN_MATCH];
    NSDictionary *param = @{
                            @"base":@3,
                            @"rules":@[@"血流成河",@"带擦挂"],
                            @"matchCode":@"1200",
                            @"playerName":@"燕洵",//NSStringFormat(@"玩家%d",arc4random() % 100),
                            @"ceiling":@4
                            };
    [HttpUtil sendRequestURL:url method:POST param:param success:^(id response) {
        NSDictionary *dic = response;
        MainRecordVC *mainRecordVC = [[MainRecordVC alloc]init];
        mainRecordVC.matchModel = [MatchModel parseDic:dic];
        [self presentViewController:mainRecordVC animated:YES completion:^{
            
        }];
    } fail:^(NSString *errorStr) {
        [Hud hudText:errorStr inView:self.view];
    }];
    
}
- (IBAction)backToFixAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

#pragma mark UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _patternModel.ruleArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SetCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kSetCollectionViewCell forIndexPath:indexPath];
    [cell.setBtn setTitle:_patternModel.ruleArr[indexPath.row] forState:UIControlStateNormal];
    return cell;
}
#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    SetCollectionViewCell *cell = (SetCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    NSString *rule = cell.setBtn.titleLabel.text;
    if ([_selectedRuleArr containsObject:rule]) {
        [_selectedRuleArr removeObject:rule];
        [cell.setBtn setImage:UIImageWithName(@"mutipleUnSelected") forState:UIControlStateNormal];
    }else{
        [_selectedRuleArr addObject:rule];
        [cell.setBtn setImage:UIImageWithName(@"mutipleSelected") forState:UIControlStateNormal];
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(133,60);
}
/**
 *  section的内边距
 */
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
/**
 *  section之间的行距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
/*
 *  item之间的间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

#pragma mark - UIPickerViewDelegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (pickerView == _basePickerView) {
        return _baseDataSource[row];
    }else{
        return _ceilingDataSource[row];
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (pickerView == _basePickerView) {
        _selectedBase = _baseDataSource[row];
    }else{
        _selectedCeiling = _ceilingDataSource[row];
    }
}

#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (pickerView == _basePickerView) {
        return _baseDataSource.count;
    }else{
        return _ceilingDataSource.count;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
